package simonby.top.annualring.application.service;

import simonby.top.annualring.domain.model.FamilyTree;
import simonby.top.annualring.domain.repository.FamilyTreeRepository;
import simonby.top.annualring.infrastructure.repository.FamilyTreeRepositoryImpl;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;

/**
 * 家族树应用服务
 * 处理家族树相关的业务逻辑
 */
@Service
public class FamilyTreeApplicationService {
    private final FamilyTreeRepository familyTreeRepository;

    public FamilyTreeApplicationService(FamilyTreeRepositoryImpl familyTreeRepository) {
        this.familyTreeRepository = familyTreeRepository;
    }

    /**
     * 创建家族树
     * @param familyTree 家族树对象
     * @return 创建后的家族树
     */
    public FamilyTree createFamilyTree(FamilyTree familyTree) {
        return familyTreeRepository.save(familyTree);
    }

    /**
     * 根据ID获取家族树
     * @param id 家族树ID
     * @return 家族树对象
     */
    public Optional<FamilyTree> getFamilyTreeById(Long id) {
        return familyTreeRepository.findById(id);
    }

    /**
     * 根据创建者ID获取家族树
     * @param creatorId 创建者ID
     * @return 家族树列表
     */
    public List<FamilyTree> getFamilyTreesByCreatorId(Long creatorId) {
        return familyTreeRepository.findByCreatorId(creatorId);
    }

    /**
     * 获取所有家族树
     * @return 家族树列表
     */
    public List<FamilyTree> getAllFamilyTrees() {
        return familyTreeRepository.findAll();
    }

    /**
     * 更新家族树
     * @param familyTree 家族树对象
     * @return 更新后的家族树
     */
    public FamilyTree updateFamilyTree(FamilyTree familyTree) {
        return familyTreeRepository.save(familyTree);
    }

    /**
     * 删除家族树
     * @param id 家族树ID
     */
    public void deleteFamilyTree(Long id) {
        familyTreeRepository.deleteById(id);
    }
}